/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Validator;

import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@ManagedBean
@RequestScoped


@FacesValidator("Validator.PasswordValidator")
public class PasswordValidator implements Validator {

    // Actions ------------------------------------------------------------------------------------

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
        throws ValidatorException
    {
        // Obtain the first password component from f:attribute.
        UIInput passwordComponent = (UIInput) component.getAttributes().get("password");

        // Get its value, the entered password of the first field.
        String password = (String) passwordComponent.getValue();

        // Cast the value of the entered password of the second field back to String.
        String confirm = (String) value;

        // Compare the first password with the second password.
        if (!password.equals(confirm)) {
            throw new ValidatorException(new FacesMessage("Passwords are not equal."));
        }

        // You can even validate the minimum password length here and throw accordingly.
        // Or, if you're smart, calculate the password strength and throw accordingly ;)
    }

}